<?php
class Model_store extends Model {

	var $_tbl_name;
	var $_tbl_pri;
	function Model_store() {
		// Call the Model constructor
		parent::Model();
		$this->_tbl_name = "tbl_store";
		$this->_tbl_pri = "id";
	}

	/**
	 *
	 *
	 */
	function find($id) {
		$this->db->where("id", $id);
		$query = $this->db->get($this->_tbl_name);

		if ($query->num_rows() > 0) {
			return $query->first_row('array');
		}
		return null;
	}

	/**
	 *
	 *
	 */
	function fetch_all(){
		$this->db->order_by("sort");
		$query = $this->db->get($this->_tbl_name);

		if ($query->num_rows() > 0) {
			return $query->result_array();
		}
		return null;
	}
	
	/**
	 * 
	 * 
	 * 
	 * @param object $keyword
	 * @return 
	 */
	function browse_count($keyword, $types){
		if(!empty($types)){
			// left join
			$this->db->join('tbl_store_type', 'tbl_stores.id = tbl_store_type.store_id', 'left');
			$this->db->group_by($this->_tbl_name.'.id');
			$this->db->where_in('tbl_store_type.type_id', $types);
		}
		
		$this->db->from($this->_tbl_name);
		if(!empty($keyword)){
			$this->db->like($this->_tbl_name.'.title', $keyword);
			$this->db->or_like($this->_tbl_name.'.company_name', $keyword);
			$this->db->or_like($this->_tbl_name.'.home_phone', $keyword);
			$this->db->or_like($this->_tbl_name.'.zipcode', $keyword);
			$this->db->or_like($this->_tbl_name.'.email', $keyword);
		}
		//echo $this->db->last_query();
		$query = $this->db->get();
		if ($query->num_rows() > 0) {
			return $query->num_rows();
		}
		return 0;
	}
	/**
	 * 
	 * 
	 * @return array() 
	 */
	function browse($keyword, $types = null, $limit = 10, $offset = 0, $order_by = 'sort', $method = "ASC"){
		$this->db->from($this->_tbl_name);
		if(!empty($types)){
			// left join
			$this->db->join('tbl_store_type', 'tbl_stores.id = tbl_store_type.store_id', 'right');
			$this->db->group_by($this->_tbl_name.'.id');
			$this->db->where_in('tbl_store_type.type_id', $types);
		}
		if(!empty($keyword)){
			$this->db->like($this->_tbl_name.'.title', $keyword);
			$this->db->or_like($this->_tbl_name.'.company_name', $keyword);
			$this->db->or_like($this->_tbl_name.'.home_phone', $keyword);
			$this->db->or_like($this->_tbl_name.'.zipcode', $keyword);
			$this->db->or_like($this->_tbl_name.'.email', $keyword);
		}
		
		$this->db->order_by($order_by, $method);
		$this->db->limit($limit, $offset);

		$query = $this->db->get();
		//echo $this->db->last_query();
		if ($query->num_rows() > 0) {
			return $query->result_array();
		}
		return null;
	}
	
	/**
	 *
	 *
	 */
	function insert_store($data)
	{
		$sort = $this->get_max_sort();
		$data['sort'] = $sort;
		$this->db->insert($this->_tbl_name, $data);
		$id = $this->db->insert_id();
		return $id;
	}

	/**
	 *
	 *
	 */
	function update_store($id, $data)
	{
		$this->db->where('id', $id);
		$data['modified_date'] = date("Y-m-d h:i:s");
		return $this->db->update($this->_tbl_name, $data);
	}

	/**
	 *
	 *
	 */
	function delete_store($id){
		// not detete item in group
		$this->db->where('id', $id);
		return $this->db->delete($this->_tbl_name);
	}

	/**
	 *
	 *
	 */
	function get_max_sort()
	{
		$this->db->select_max('sort');
		$query = $this->db->get($this->_tbl_name);
		if ($query->num_rows() > 0) {
			$first_row = $query->first_row('array');
			return $first_row['sort'];
		}
		return 0;
	}

	/**
	 *
	 *
	 */
	function check_email($email, $id){
		$this->db->where('email', $email);
		if(intval($id)){
			$this->db->where_not_in('id', $id);
		}
		$query = $this->db->get($this->_tbl_name);
		//echo $this->db->last_query();
		if ($query->num_rows() > 0) {
			return $query->first_row('array');
		}
		return null;
	}
}
?>